//
//  OrderTimeView.m
//  YaDiuDiu
//
//  Created by 陈刚 on 2017/9/30.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "OrderTimeView.h"
#import "CompletionStatusTableViewCell.h"
#import "AllClassIfyModel.h"

@interface OrderTimeView ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic, strong) UITableView *orderTimeTable;

@property (nonatomic, strong) UIView *bgView;

@property (nonatomic, strong) NSMutableArray *orderTimeArray;


@end

@implementation OrderTimeView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self setupView];
        
        [self addConstraint];
        
        [self loadData];
    }
    return self;
}

- (void)setupView{
    UITapGestureRecognizer *gest = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cancelEcent:)];
    
    self.bgView = [[UIView alloc]init];
    [self.bgView setBackgroundColor:[UIColor blackColor]];
    [self.bgView setAlpha:0.5];
    [self.bgView setUserInteractionEnabled:YES];
    [self.bgView addGestureRecognizer:gest];
    [self addSubview:self.bgView];
    
    self.orderTimeTable = [[UITableView alloc]init];
    [self.orderTimeTable setDataSource:self];
    [self.orderTimeTable setDelegate:self];
    [self.orderTimeTable setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    [self.orderTimeTable setBackgroundColor:[UIColor clearColor]];
    [self.orderTimeTable registerClass:[CompletionStatusTableViewCell class] forCellReuseIdentifier:@"Cell"];
    [self addSubview:self.orderTimeTable];
}

- (void)addConstraint{
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self);
    }];
    
    [self.orderTimeTable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(0);
        make.left.equalTo(0);
        make.right.equalTo(0);
        make.height.equalTo(264);
    }];
}

- (void)loadData{
    
    NSArray *titleArray = [[NSArray alloc]initWithObjects:@"全部时间",@"今天",@"本周内",@"本月内",@"三个月内",@"一年内", nil];
    for (int i = 0; i< titleArray.count; i ++) {
        AllClassIfyModel *model = [[AllClassIfyModel alloc]init];
        model.title = [titleArray objectAtIndex:i];
        if (i == 0) {
            model.isSel = YES;
            model.value = [NSString stringWithFormat:@"%zd",i];
        }else{
            model.value = [NSString stringWithFormat:@"%zd",i+1];
        }
        [self.orderTimeArray addObject:model];
    }
    
    [self.orderTimeTable reloadData];
}

#pragma mark ======UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.orderTimeArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identitfer = @"Cell";
    CompletionStatusTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identitfer];
    if (cell == nil) {
        cell = [[CompletionStatusTableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identitfer];
    }
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    AllClassIfyModel *itemModel = [self.orderTimeArray objectAtIndex:indexPath.row];
    [cell setcompleData:itemModel];
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AllClassIfyModel *itemModel = [self.orderTimeArray objectAtIndex:indexPath.row];
    for (int i = 0; i< self.orderTimeArray.count; i ++) {
        AllClassIfyModel *model = [self.orderTimeArray objectAtIndex:i];
        if (i == indexPath.row) {
            model.isSel = YES;
        }else{
            model.isSel = NO;
        }
    }
    [self.orderTimeTable reloadData];
    self.selectAction(itemModel);
}

- (NSMutableArray *)orderTimeArray{
    if (!_orderTimeArray) {
        _orderTimeArray = [[NSMutableArray alloc]init];
    }
    return _orderTimeArray;
}

- (void)cancelEcent:(UITapGestureRecognizer *)gest{
    self.cancelAction();
}

@end
